package com.xuexiang.xui.widget.divider;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

/**
 * LinearLayoutManager,空白分隔（底部带分隔线）
 *
 * @author LiuMeng
 * @date On 2022/5/31
 */
public class SpaceItemDecoration extends RecyclerView.ItemDecoration {
    private int mFirstItemDecoration;
    private int mLeftDecoration;
    private int mRightDecoration;
    private int mTopDecoration;
    private int mBottomDecoration;
    private int mOrientation;
    //orientation 水平
    public static final int HORIZONTAL = 0;
    //orientation 垂直
    public static final int VERTICAL = 1;

    public SpaceItemDecoration(int orientation, int firstItemDecoration, int itemDecoration) {
        this.mOrientation = orientation;
        if (mOrientation == HORIZONTAL) {
            this.mFirstItemDecoration = firstItemDecoration;
            this.mRightDecoration = itemDecoration;
        } else {
            this.mFirstItemDecoration = firstItemDecoration;
            this.mBottomDecoration = itemDecoration;
        }
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        if (mOrientation == HORIZONTAL) {
            outRect.right = mRightDecoration;
            outRect.bottom = mBottomDecoration;
            outRect.top = mTopDecoration;
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.left = mFirstItemDecoration;
            }
        } else if (mOrientation == VERTICAL) {
            outRect.right = mRightDecoration;
            outRect.bottom = mBottomDecoration;
            outRect.left = mLeftDecoration;
            if (parent.getChildAdapterPosition(view) == 0) {
                outRect.top = mFirstItemDecoration;
            }
        }
    }

}

